package com.mall.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.pageResult.PageResult;
import com.mall.pojo.PmsProductCategory;
import com.mall.service.PmsProductCategoryService;
import com.mall.utils.Result;
import com.mall.vo.PmsProductCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {

    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;

    //添加商品分类
    @PostMapping("/create")
    public Result<PmsProductCategory> create(@RequestBody PmsProductCategory pmsProductCategory) {
        pmsProductCategoryService.create(pmsProductCategory);
        return Result.success(null, "操作成功");
    }

    //删除商品分类
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        pmsProductCategoryService.delete(id);
        return Result.success(null, "操作成功");
    }

    //查询所有一级分类及子分类
    @GetMapping("/list/withChildren")
    public Result<List<PmsProductCategoryVo>> withChildren() {
       List<PmsProductCategoryVo> pmsProductCategoryVo = pmsProductCategoryService.withChildren();
        return Result.success(pmsProductCategoryVo, "操作成功");
    }

    //分页查询商品分类
    @GetMapping("list/{parentId}")
    public Result<PageResult<PmsProductCategory>> list(
            @PathVariable("parentId") Integer parentId,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageResult<PmsProductCategory> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        Page<PmsProductCategory> page = pmsProductCategoryService.list(parentId, pageNum, pageSize);
        if (CollUtil.isEmpty(page.getRecords())) {
            pageResult.setList(new ArrayList<>());
            return Result.success(pageResult, "操作成功");
        }
        pageResult.setList(page.getRecords());
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        return Result.success(pageResult, "操作成功");
    }

    //修改导航栏显示状态
    @PostMapping("/update/navStatus")
    public Result<PmsProductCategory> navStatus(@RequestParam("ids") List<Integer> ids,
                                                @RequestParam("navStatus") Integer navStatus) {
        pmsProductCategoryService.navStatus(ids, navStatus);
        return Result.success(null, "操作成功");
    }

    //修改显示状态
    @PostMapping("/update/showStatus")
    public Result<PmsProductCategory> showStatus(@RequestParam("ids") List<Integer> ids,
                                                 @RequestParam("showStatus") Integer showStatus) {
        pmsProductCategoryService.showStatus(ids, showStatus);
        return Result.success(null, "操作成功");
    }

    //修改商品分类
    @PostMapping("/update/{id}")
    public Result<PmsProductCategory> update(@RequestParam("id") List<Integer> id,
                                             @RequestBody PmsProductCategory pmsProductCategory) {
        pmsProductCategoryService.update(id, pmsProductCategory);
        return Result.success(null, "操作成功");
    }

    //根据id获取商品分类
    @GetMapping("/{id}")
    public Result<PmsProductCategory> selectById(@PathVariable("id") Integer id) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryService.selectById(id);
        return Result.success(pmsProductCategory, "操作成功");
    }

}
